﻿using Microsoft.Extensions.DependencyInjection;
using System.Reflection;

namespace HelperFromTheSkeleton.ExtendMethod
{
    public static class IServiceCollectionExtend
    {
        /// <summary>
        /// 加载集合
        /// </summary>
        /// <param name="AssemblyName"></param>

        public static void AddAssembly(this IServiceCollection serviceDescriptors, string assemblyName, string like, ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
        {
            ///IWeChatPayRepository
            Assembly ab;
            ab = Assembly.Load($"{assemblyName}");
            var types = ab.GetExportedTypes();
            if (types == null) return;
            foreach (var val in types)
            {
                if (!string.IsNullOrWhiteSpace(like))
                {
                    if (val.Name.Contains($"{like}"))
                    {
                        var inteficeTypes = val.GetInterfaces();
                        if (inteficeTypes != null && inteficeTypes.Count()>0)
                        {
                            foreach (var inteficeType in inteficeTypes)
                            {
                                switch (serviceLifetime)
                                {
                                    case ServiceLifetime.Transient:
                                        serviceDescriptors.AddTransient(inteficeType, val);
                                        break;
                                    case ServiceLifetime.Singleton:
                                        serviceDescriptors.AddSingleton(inteficeType, val);
                                        break;
                                    case ServiceLifetime.Scoped:
                                        serviceDescriptors.AddScoped(inteficeType, val);
                                        break;
                                }
                            }
                        }
                        else
                        {
                            switch (serviceLifetime)
                            {
                                case ServiceLifetime.Transient:
                                    serviceDescriptors.AddTransient(val);
                                    break;
                                case ServiceLifetime.Singleton:
                                    serviceDescriptors.AddSingleton(val);
                                    break;
                                case ServiceLifetime.Scoped:
                                    serviceDescriptors.AddScoped(val);
                                    break;
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// AutoMapper配置文件加载
        /// </summary>
        /// <param name="serviceDescriptors"></param>
        /// <param name="AssemblyName"></param>
        /// <param name="like"></param>
        public static void AddAutoMapper(this IServiceCollection serviceDescriptors, string AssemblyName, string like)
        {
            Assembly ab;
            ab = Assembly.Load($"{AssemblyName}");
            var types = ab.GetExportedTypes();
            if (types == null) return;
            foreach (var val in types)
            {
                if (!string.IsNullOrWhiteSpace(like))
                {
                    if (val.Name.Contains($"{like}"))
                    {
                        serviceDescriptors.AddAutoMapper(val);
                    }
                }
            }
        }
    }
}